package com.estore.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnection {
    public static Connection conn = null;

    static {
        InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        // 加载jdbc的配置文件
        try {
            properties.load(is);
            Class.forName(properties.getProperty("jdbc.driver"));
            conn = DriverManager.getConnection(
                    properties.getProperty("jdbc.url"),
                    properties.getProperty("jdbc.username"),
                    properties.getProperty("jdbc.password"));
            System.out.println("数据库连接成功");
        } catch (IOException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            System.err.println("数据库连接错误");
            throw new RuntimeException(e);

        }
    }


    public static Connection getConnection() {
        return conn;
    }


}

